import { CodeTable } from '@renderer/components/J-Table/CodeTable';

/** 须用ref，不能用shallowRef，否则分隔符等无法双向绑定
 * CodeTable、ListTable全局都须要用，所以Store
 */
const store = {
  codeTable: ref<CodeTable>(),
  saveAndRepair: () => {
    if( store.codeTable.value ){
      store.codeTable.value.update().then( () => {
        if( store.codeTable.value ){
          store.fetchDict( store.codeTable.value.data.filePath );
        }
      } );
    }
  },
  fetchDict: async ( filePath: string ) => {
    await window.api.fetchDict( filePath ).then( tableData => {
      store.codeTable.value = new CodeTable( tableData );
    } );
  }
};


export function useStore(){
  return store;
}
